package com.atguigu.flink.chapter05.source;

import com.atguigu.flink.bean.Person;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.serialization.DeserializationSchema;
import org.apache.flink.api.common.typeinfo.TypeHint;
import org.apache.flink.api.common.typeinfo.TypeInformation;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.connector.kafka.source.KafkaSource;
import org.apache.flink.connector.kafka.source.enumerator.initializer.OffsetsInitializer;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.io.IOException;

/**
 * @Author lzc
 * @Date 2023/6/17 15:27
 */
public class KafkaSourceDemo2 {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(2);  // 一般和 kafka 的分区数保持一致
    
        KafkaSource<Person> source = KafkaSource.<Person>builder()
            .setBootstrapServers("hadoop162:9092,hadoop163:9092,hadoop163:9092")
            .setGroupId("atguigu")
            .setTopics("s1")
            .setStartingOffsets(OffsetsInitializer.latest())
            .setValueOnlyDeserializer(new DeserializationSchema<Person>() {
                
                // 反序列化
                @Override
                public Person deserialize(byte[] message) throws IOException {
                    String[] data = new String(message).split(",");
                    return new Person(Integer.parseInt(data[0]), data[1]);
                }
                
                // 是否结束流
                @Override
                public boolean isEndOfStream(Person nextElement) {
                    return false;
                }
    
                @Override
                public TypeInformation<Person> getProducedType() {
                    // return Types.STRING;  //  常见的一些类型
                    //return TypeInformation.of(Person.class);  // 适合自定义的类型(没有泛型)
                    return TypeInformation.of(new TypeHint<Person>() {}); // 适合所有类型
                }
                
            })
            .build();
    
        DataStreamSource<Person> stream = env.fromSource(source, WatermarkStrategy.noWatermarks(), "kafka-source");
        stream.print();
    
    
        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
